package com.itheima.day2.exercise;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class Exercise3 {
    public static void main(String[] args) {
        //4242424
        List<Integer> list = Arrays.asList(1,2,3);
        //1.predicate
        List<Integer> result = filter1(list, a -> (a & 1) == 0);
        System.out.println(result);
        //2.function
        List<String> strings = map1(list, t -> String.valueOf(t));
        //3.consumer
        consume(list, number -> System.out.println(number));
        //4.supplier
        supply(5, () -> ThreadLocalRandom.current().nextInt());
    }

    static List<Integer> filter(List<Integer> list) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : list) {
            /*
            0 10 110 110 1110
            01
            */
            if ((number & 1) == 0) {
                result.add(number);
            }
        }
        return result;
    }

    static List<Integer> filter1(List<Integer> list, Predicate<Integer> integerPredicate) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : list) {
            /*
            0 10 110 110 1110
            01
            */
//            if ((number & 1) == 0) { //规则可能改变
//                result.add(number);
//            }
            if (integerPredicate.test(number)) {
                result.add(number);
            }
        }
        return result;
    }


    static List<Integer> filter(List<Integer> list, Predicate<Integer> predicate) {
        List<Integer> result = new ArrayList<>();
        for (Integer number : list) {
            // 筛选：判断是否是偶数，但以后可能改变筛选规则
            if(predicate.test(number)) {
                result.add(number);
            }
        }
        return result;

        /*
            (Integer number) -> (number & 1) == 0
         */
    }

    static List<String> map(List<Integer> list) {
        List<String> result = new ArrayList<>();
        for (Integer number : list) {
            // 转换：将数字转为字符串，但以后可能改变转换规则
            result.add(String.valueOf(number));
        }
        return result;
    }

    static List<String> map1(List<Integer> list, Function<Integer, String> func) {
        List<String> result = new ArrayList<>();//
        for (Integer number : list) {
//            // 转换：将数字转为字符串，但以后可能改变转换规则
//            result.add(String.valueOf(number));
            result.add(func.apply(number));
        }
        return result;
    }

    static void consume(List<Integer> list, Consumer<Integer> consumer) {
        for (Integer number : list) {
            // 消费：打印，但以后可能改变消费规则
//            System.out.println(number);
            consumer.accept(number);
        }
    }

    static List<Integer> supply(int count, Supplier<Integer> supplier) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            // 生成：随机数，但以后可能改变生成规则
//            result.add(ThreadLocalRandom.current().nextInt());
            result.add(supplier.get());
        }
        return result;
    }
}
